package com.mvvm.start.base.base

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.mvvm.start.base.utils.ViewModelUtils
import com.mvvm.start.base.utils.getViewBinding
import com.mvvm.start.base.utils.toast
import com.zyao89.view.zloading.ZLoadingDialog
import com.zyao89.view.zloading.Z_TYPE

/**
 * @author lmy
 * @功能:
 * @Creat 2020/11/6 1:21 PM
 * @Compony 465008238@qq.com
 */
abstract class BaseFragment<VM : BaseViewModel, VB : ViewDataBinding>(
    private val shareViewModel: Boolean = false,//是否共享Activity中的ViewModel
    private val factory: ViewModelProvider.Factory? = null
) : Fragment() {
    private var mContext: Context? = null
    protected lateinit var mViewModel: VM
    private var isFirstLoad = true // 是否第一次加载
    protected var isFirstRequest: Boolean = true//设置进入界面是否初始化数据
    private lateinit var loadingDialog: ZLoadingDialog
    protected lateinit var viewBinding: VB
        private set

    /**
     * 初始化ViewBinding
     */
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewBinding = getViewBinding(inflater, container)
        viewBinding.lifecycleOwner = this
        mViewModel = if (shareViewModel) ViewModelUtils.createActivityViewModel(this, factory, 0)
        else ViewModelUtils.createViewModel(this, factory, 0)
        return viewBinding.root
    }

    /**
     *初始化
     */
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initBasicsObserve()
        initObserve()
        if (isFirstRequest) {
            initData()
        }
    }

    private fun initBasicsObserve() {
        mViewModel.apply {
            toastText.observe(viewLifecycleOwner) {
                it.toast()
            }
            isLoading.observe(viewLifecycleOwner) {
                if (it) showLoadProgress() else hideLoadProgress()
            }
        }
    }


    /**
     * 初始化View
     */
    abstract fun initViews()
    abstract fun initObserve()
    abstract fun initData()

    override fun onDestroy() {
        super.onDestroy()
        mContext = null
        //此处记得取消绑定，避免内存泄露
        if (::viewBinding.isInitialized) {
            viewBinding.unbind()
        }
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    /**
     * 显示加载框
     */
    open fun showLoadProgress() {
        mContext?.let {
            if (!::loadingDialog.isInitialized) {
                loadingDialog = ZLoadingDialog(mContext!!)
                loadingDialog.apply {
                    setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE) //设置类型
                    setLoadingColor(Color.GRAY) //颜色
                    setHintText("加载中...")
                        .setCancelable(true)
                    setHintTextSize(16f) // 设置字体大小 dp
                    setHintTextColor(Color.GRAY) // 设置字体颜色
                    setDurationTime(0.5) // 设置动画时间百分比 - 0.5倍
//                setDialogBackgroundColor(Color.parseColor("#CC111111")); // 设置背景色，默认白色
                }
            }
            loadingDialog.apply {
                show()
            }
        }
    }

    /**
     * 隐藏加载框
     */
    open fun hideLoadProgress() {
        mContext?.let {
            if (::loadingDialog.isInitialized) {
                loadingDialog.apply {
                    dismiss()
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            isFirstLoad = false
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        isFirstLoad = false
    }

}